package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.cl.pojo.User;
import com.cl.service.impl.UserServiceImpl;
import com.cl.utils.CountUtil;
import com.cl.utils.MsgUtil;
import com.cl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class LoginController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private MsgUtil msgUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CountUtil countUtil;
    @PostMapping("/login")
    public String login(@RequestParam("email")String email, @RequestParam("pwd")String pwd, HttpServletRequest request){
        User user = userService.queryByEmail(email);
        if (user==null){
            return JSON.toJSONString(msgUtil.fail("账号错误"));
        }
        redisUtil.set(user.getuName(),countUtil.getCount(),20);
        if (countUtil.getCount()>=5&&(redisUtil.get(user.getuName())!=null)){
            return JSON.toJSONString(msgUtil.fail("密码错误次数太多，请稍后再试"));
        }
        if (!user.getuPwd().equals(pwd)){
            countUtil.setCount(countUtil.getCount()+1);
            redisUtil.getAndSet(user.getuName(),countUtil.getCount());
            return JSON.toJSONString(msgUtil.fail("密码错误"));
        }
        countUtil.setCount(0);
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        return JSON.toJSONString(user);
    }
    @PostMapping("/quit")
    public String quit(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return JSON.toJSONString(msgUtil.success("退出成功"));
    }
    @PostMapping("/modifyPwd")
    public String modifyPwd(HttpServletRequest request,@RequestParam("newPwd")String newPwd,@RequestParam("code")String code){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String code1 = (String) session.getAttribute("code");
        if (user==null){
            return JSON.toJSONString(msgUtil.fail("修改失败"));
        }
        if (!code.toUpperCase().equals(code1.toUpperCase())){
            return JSON.toJSONString(msgUtil.fail("验证码错误"));
        }
        String name = user.getuName();
        int i = userService.modifyPwd(name, newPwd);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("修改成功"));
        }else return JSON.toJSONString(msgUtil.fail("修改失败"));
    }
    @PostMapping("/modifyAdminPwd")
    public String modifyAdminPwd(HttpServletRequest request,@RequestParam("newPwd")String newPwd){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user==null){
            return JSON.toJSONString(msgUtil.fail("修改失败"));
        }
        int i = userService.modifyPwd(user.getuName(), newPwd);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("修改成功"));
        }else return JSON.toJSONString(msgUtil.fail("修改失败"));
    }
}
